package com.dream.controller;

import com.dream.bean.Cart;
import com.dream.bean.Order;
import com.dream.bean.OrderItem;
import com.dream.bean.User;
import com.dream.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 匠人码农
 */
@RequestMapping("/order")
@Controller
public class OrderController {

    /**
     * 支付页面
     */
    private static final String PAY = "redirect:/pages/pay/pay.jsp";

    /**
     * 登录页面
     */
    private static final String LOGIN = "/user/login";

    /**
     * 订单页面
     */
    private static final String ORDER = "/order/order";

    /**
     * 订单详细页面
     */
    private static final String ORDER_DETAIL = "/order/order_detail";

    /**
     * 订单管理页面
     */
    private static final String ORDER_MANAGER = "/manager/order_manager";

    /**
     * 支付完成页面
     */
    private static final String CHECKOUT = "/pay/checkout";

    /**
     * 订单service声明
     */
    @Autowired
    OrderService orderService;

    /**
     * 创建订单
     * @param req 请求
     * @return  订单页面
     */
    @RequestMapping(value = "/createOrder")
    protected String createOrder(HttpServletRequest req){

        //获取购物车
        Cart cart = (Cart) req.getSession().getAttribute("cart");

        //获取用户信息
        User user = (User) req.getSession().getAttribute("user");

        if(user == null){
            //跳转到的登录页面
            return LOGIN;
        }

        //调用service
        if(cart != null){

            String orderId = orderService.createOrder(cart, user.getId());

            //回传数据
            req.getSession().setAttribute("orderId",orderId);

            //使用重定向返回页面
            return PAY;
        }

        return "redirect:" + req.getHeader("Referer");

    }

    /**
     * 查看我的所有订单信息
     * @param model model
     * @param req   请求
     * @return      所有订单页面
     */
    @RequestMapping("/showMyOrders")
    protected String showMyOrders(Model model, HttpServletRequest req){

        //获取用户ID
        User user = (User) req.getSession().getAttribute("user");

        //登录的情况
        if(user != null) {
            //查看
            List<Order> orderList = orderService.showMyOrders(user.getId());

            //回传数据
            model.addAttribute("orderList", orderList);

            //返回订单页面
            return ORDER;

        }else{
            //返回登录页面
            return LOGIN;
        }

    }

    /**
     * 查看该订单详情
     * @param orderId 订单号
     * @param model   model
     * @return        订单详情页面
     */
    @RequestMapping("/showOrderDetail")
    protected String showOrderDetail(@RequestParam(value="orderId") String orderId, Model model){

        //查看
        List<OrderItem> orderItemList = orderService.showOrderDetail(orderId);

        //回传数据
        model.addAttribute("orderItemList", orderItemList);

        //返回订单详细页面
        return ORDER_DETAIL;

    }

    /**
     * 管理员查看所有订单
     * @param model model
     * @return      所有订单页面
     */
    @RequestMapping("/showAllOrders")
    protected String showAllOrders(Model model){

        //查看
        List<Order> allOrder = orderService.showAllOrders();

        //回传数据
        model.addAttribute("allOrder", allOrder);

        //返回订单管理页面
        return ORDER_MANAGER;

    }

    /**
     * 发货
     * @param orderId  订单号
     * @param model    model
     * @return         订单管理页面
     */
    @RequestMapping("/sendOrder")
    protected String sendOrder(@RequestParam(value = "orderId") String orderId,Model model){

        //发货
        orderService.sendOrder(orderId);

        //查新最新数据
        List<Order> allOrder = orderService.showAllOrders();

        //回传数据
        model.addAttribute("allOrder", allOrder);

        //返回订单管理页面
        return ORDER_MANAGER;

    }

    /**
     * 签收订单
     * @param orderId 订单号
     * @param model   model
     * @param req     请求
     * @return        订单页面
     */
    @RequestMapping("/signOrder")
    protected String signOrder(@RequestParam(value="orderId") String orderId,Model model,HttpServletRequest req){

        //获取用户
        User user= (User) req.getSession().getAttribute("user");

        //签收订单
        orderService.signOrder(orderId);

        //查看
        List<Order> orderList = orderService.showMyOrders(user.getId());

        //回传数据
        model.addAttribute("orderList", orderList);

        //返回页面
        return ORDER;

    }

    /**
     * 支付
     * @param orderId 订单号
     * @param req     请求
     * @return        支付成功页面
     */
    @RequestMapping("/payOrder")
    protected String payOrder(@RequestParam(value = "orderId") String orderId, HttpServletRequest req){

        //支付
        orderService.payOrder(orderId);

        //获取购物车
        Cart cart = (Cart) req.getSession().getAttribute("cart");

        if(cart != null) {
            //支付完的东西清空购物车
            req.getSession().removeAttribute("cart");
        }

        //回传数据
        req.getSession().setAttribute("orderId", orderId);

        //返回支付完成页面
        return CHECKOUT;

    }
}
